package org.jeecg.modules.appuser.api.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.mall.entity.TbMnAdzone;
import org.jeecg.modules.mall.entity.TbMnOrder;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnAdzoneService;
import org.jeecg.modules.mall.service.ITbMnOrderService;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 描述
 * </p>
 */
@Slf4j
@Api(tags = "app淘宝")
@RestController
@RequestMapping("/app/taobao/api")
public class OrderController {

    @Autowired
    @Lazy(true)
    private ITbMnUserService tbMnUserService;

    @Autowired
    @Lazy(true)
    private ITbMnAdzoneService tbMnAdzoneService;

    @Autowired
    private ITbMnOrderService tbMnOrderService;

    @AutoLog("查询淘宝的订单信息")
    @ApiOperation(value = "查询淘宝的订单信息", notes = "订单查询")
    @PostMapping(value = "/queryOrders")
    public Result<?> queryOrders(TbMnOrder tbMnOrder,String user_id, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletResponse response, HttpServletRequest request) {
        //获取当前登陆的账号，查出广告位id
        Map<String, Object> resultMap = new HashMap<>();
        //TODO  这里从redis取出账号
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account", user_id);
        TbMnAdzone tbMnAdzone = tbMnAdzoneService.getOne(queryWrapper);
        if (null == tbMnAdzone) {
            resultMap.put("state", 0);
            resultMap.put("msg", "推广位错误");
            return Result.ok(resultMap);
        }


        TbMnUser tbMnUser = tbMnUserService.getById(user_id);
        List<String> adzoneIds = new ArrayList<>();
        String adzoneid=tbMnAdzone.getAdzoneId();
        adzoneid=adzoneid.trim();
        if(adzoneid.indexOf("l")>-1){
            adzoneid=adzoneid.substring(0,adzoneid.length()-1);
        }
        adzoneIds.add(adzoneid);
//        tbMnUser.setAgencyId(tbMnUser.getId());
//        tbMnOrder.setAdzoneId(tbMnAdzone.getAdzoneId());
//        if ("2".equals(tbMnUser.getIsAgency())) {//如果是代理，则需要查询出团队的订单
//            QueryWrapper agencyQueryWrapper = new QueryWrapper();
//            agencyQueryWrapper.eq("agency_id", tbMnUser.getId());
//            List<TbMnUser> list = tbMnUserService.list(agencyQueryWrapper);
//            for (TbMnUser userDo : list) {
//                adzoneIds.add(userDo.getId());//获取团队广告位id
//            }
//        }

        List<TbMnOrder> tbMnOrderList = tbMnOrderService.queryOrderByStatus(tbMnOrder, adzoneIds, pageNo, pageSize, tbMnUser);
        return Result.ok(tbMnOrderList);
    }
}
